package cn.jarlen.jwt.tools.x509;

import android.content.Context;

import java.io.InputStream;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

public class X509Utils {

    /**
     * 获取X509证书
     *
     * @param mContext
     * @return
     */
    public static X509Certificate makeX509ByDefault(Context mContext) {
        X509Certificate x509 = null;
        InputStream in = null;
        try {
            in = mContext.getAssets().open("test.cer");
            x509 = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(in);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return x509;
    }

    /**
     * 获取X509证书
     *
     * @param mContext
     * @return
     */
    public static X509Certificate makeX509ByBCProvider(Context mContext) {

        X509Certificate x509 = null;
        InputStream in = null;
        try {
            in = mContext.getAssets().open("test.cer");
            x509 = (X509Certificate) CertificateFactory.getInstance("X.509", "BC").generateCertificate(in);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return x509;
    }

}
